package com.qs.eggs.view.activity;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.qs.eggs.Constants;
import com.qs.eggs.R;
import com.qs.eggs.adapter.CollectAdapter;
import com.qs.eggs.base.activity.BaseActivity;
import com.qs.eggs.bean.CollectBean;
import com.qs.eggs.refresh.widget.RecyclerRefreshLayout;

import java.lang.reflect.Type;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Response;

public class CollectActivity extends BaseActivity implements View.OnClickListener {
    public static final int ACTION_EDIT=1;
    public static final int ACTION_CAMPLATE=2;
    private RecyclerView recyclerView;
    private CollectAdapter collectAdapter;
    private TextView mEdit;
    protected RecyclerRefreshLayout mRefreshLayout;
    protected boolean isRefreshing;
    @Override
    protected int getContentView() {
        return R.layout.activity_collect;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        recyclerView=(RecyclerView)findViewById(R.id.collect_recyclerview);
        mEdit=(TextView)findViewById(R.id.tv_edit);
        mEdit.setOnClickListener(this);
        mEdit.setTag(ACTION_EDIT);
    }

    @Override
    protected void initData() {
        super.initData();
        OkGo.post(Constants.URL_BASE+"collect?action=searchCollection")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {

                        Type type = new TypeToken<ArrayList<CollectBean>>() {
                        }.getType();

                        GsonBuilder gsonBuilder = new GsonBuilder();
                        ArrayList<CollectBean> resultBean = gsonBuilder.create().fromJson(s, type);
                        collectAdapter=new CollectAdapter(CollectActivity.this,resultBean,mEdit);
                        recyclerView.setAdapter(collectAdapter);
                        recyclerView.setLayoutManager(new LinearLayoutManager(CollectActivity.this));
                        recyclerView.setItemAnimator(new DefaultItemAnimator());

                    }
                });
        }

    @Override
    public void onClick(View v) {
        int action=(int)v.getTag();
        if(collectAdapter.getDatas().size()==0){
            Toast.makeText(CollectActivity.this,"暂无收藏",Toast.LENGTH_SHORT).show();
          return;
        }
        switch (action){
            case ACTION_EDIT:
                showDelControl();
                collectAdapter.setEdit(ACTION_EDIT);
                break;
            case ACTION_CAMPLATE:
                hideDelControl();
                collectAdapter.setEdit(ACTION_CAMPLATE);

                break;
        }
    }

    private void showDelControl(){
        mEdit.setText("完成");
        mEdit.setTag(ACTION_CAMPLATE);

    }

    private void  hideDelControl(){

        mEdit.setText("编辑");
        mEdit.setTag(ACTION_EDIT);

    }
}
